package gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import pl.CC;
import pl.Simplex;

public class SimplexTable extends JFrame {
	private JTable simplexTable;
	private Simplex simplex;
	private boolean stepCount;
	private TableModel model;
	private JButton next;
	private JButton reset;
	private JButton showResult;
	private boolean finish = false;
	private int cols;
	private int rows;
	private LogFrame log;
	private CC cc;

	public SimplexTable(Simplex s, boolean step, LogFrame l, CC c) {
		log = l;
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.simplex = s;
		this.cc = c;
		cols = simplex.getCel().length + 4;
		rows = simplex.getConditionNumber() + 4;
		model = new DefaultTableModel(rows, cols);
		setModel();
		simplexTable = new JTable(model);
		add(simplexTable);
		simplexTable.setBounds(0, 0, model.getColumnCount() * 100,
				model.getRowCount() * 25);
		simplexTable.setEnabled(false);
		setSize(model.getColumnCount() * 100, (model.getRowCount() * 25) + 100);
		stepCount = step;
		reset = new JButton("Nowe zadanie");
		add(reset);
		reset.setBounds(260, getHeight() - 100, 120, 30);
		reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						MainWindow window = new MainWindow();
						window.setVisible(true);
						dispose();
						log.dispose();
					}
				});

			}
		});
		if (stepCount) {
			setButton();
		}
	}

	private void setButton() {
		next = new JButton("Następny krok");
		add(next);
		next.setBounds(120, getHeight() - 100, 120, 30);
		next.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					finish = simplex.firstSimplex();
				} catch (NullPointerException e) {
					JOptionPane.showMessageDialog(SimplexTable.this,
							e.getMessage());
					next.setVisible(false);
				}
				setModel();
				if (finish) {
					next.setVisible(false);
					showResult.setVisible(true);
				}
			}
		});
		showResult = new JButton("Pokaż wyniki");
		add(showResult);
		showResult.setBounds(400, getHeight() -100, 120, 30);
		showResult.setVisible(false);
		showResult.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						cc.setResults(simplex);
						ResultWindow window = new ResultWindow(cc);
						window.setVisible(true);
						dispose();
						log.dispose();
					}
				});
			}
		});

	}

	public void setModel() {
		for (int i = 0; i < rows; i++) {
			if (i == 0) {
				for (int j = 0; j < (simplex.getCel().length - simplex
						.getArtificialVariablesCount()); j++) {
					model.setValueAt("x" + String.valueOf(j + 1), i, j + 2);
					simplex.setCelNames("x" + String.valueOf(j + 1), j);
				}
				for (int j = 0; j < simplex.getArtificialVariablesCount(); j++) {
					model.setValueAt(
							"S" + String.valueOf(j + 1),
							i,
							j
									+ 2
									+ (simplex.getCel().length - simplex
											.getArtificialVariablesCount()));
					simplex.setCelNames(
							"S" + String.valueOf(j + 1),
							j
									+ (simplex.getCel().length - simplex
											.getArtificialVariablesCount()));
				}
			} else if (i == 1) {
				model.setValueAt("Cj ->", i, 1);
				model.setValueAt("Bi", i, cols - 2);
				model.setValueAt("Bi/Aik", i, cols - 1);
				for (int j = 0; j < simplex.getCel().length; j++) {
					model.setValueAt(String.valueOf(simplex.getCel()[j]), i,
							j + 2);
				}
			} else if (i == (rows - 2)) {
				model.setValueAt("Zj", i, 1);
				model.setValueAt("z(x)", i, cols - 2);
				model.setValueAt(simplex.getEndValue(), i, cols - 1);
				for (int j = 0; j < simplex.getzJ().length; j++) {
					model.setValueAt(String.valueOf(simplex.getzJ()[j]), i,
							j + 2);
				}
			} else if (i == (rows - 1)) {
				model.setValueAt("Cj - Zj", i, 1);
				for (int j = 0; j < simplex.getSimplexCriterium().length; j++) {
					model.setValueAt(
							String.valueOf(simplex.getSimplexCriterium()[j]),
							i, j + 2);
				}
			} else {

				model.setValueAt(model.getValueAt(0,
						(int) simplex.getBaseValues()[i - 2][0] + 2), i, 0);
				model.setValueAt(
						String.valueOf(simplex.getBaseValues()[i - 2][1]), i, 1);
				for (int j = 0; j < simplex.getCel().length; j++) {
					model.setValueAt(
							String.valueOf(simplex.getSimplexTable()[i - 2][j]),
							i, j + 2);
				}
				model.setValueAt(
						String.valueOf(simplex.getOgrValues()[0][i - 2]), i,
						cols - 2);
				if (simplex.getOgrValues()[1][i - 2] == Simplex.IMPOSSIBLE_DIV) {
					model.setValueAt("-", i, cols - 1);
				} else {
					model.setValueAt(
							String.valueOf(simplex.getOgrValues()[1][i - 2]),
							i, cols - 1);
				}
			}
		}
	}

}
